package com.bhkj.pdjhforotherapp.core.jhaudio;

import com.bhkj.pdjhforotherapp.R;
import com.bhkj.pdjhforotherapp.common.bean.JhAudioBean;
import com.bhkj.pdjhforotherapp.common.utils.Tools;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.mine.vioceplaylib.sound.ISoundCallback;
import com.mine.vioceplaylib.sound.SoundPlayUtils;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

public class JhAudioSchduledExcutor implements Runnable {
//    private TextToSpeech tts;
//    private List<JhAudioBean> listAudio;

    JhAudioSchduledExcutor() {

//        listAudio = new ArrayList<>();
//        tts = new TextToSpeech(App.getCtx(), this);
    }

    @Override
    public void run() {

        String deviceId = Tools.getDeviceId();
        EasyHttp.get("takenum/getBoraQueue")
                .params("key", deviceId)
                .params("rsa", Tools.encodeRsa(deviceId))
                .params("deptid", StringUtils.getString(R.string.deptId))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        LogUtils.e(e.getMessage() + e.getCode());
                    }

                    @Override
                    public void onSuccess(String res) {
                        LogUtils.i(res);
                        JhAudioBean jhAudioBean = GsonUtils.getGson().fromJson(res, JhAudioBean.class);
                        if (jhAudioBean.isSuccess()) {
                            if (ObjectUtils.isNotEmpty(jhAudioBean.getDatas())) {
                                LogUtils.e(res);
                            }


                            for (JhAudioBean.DatasBean param : jhAudioBean.getDatas()) {
                                Tools.printLog("叫号获取成功", "排队号码:" + param.getTakenum() + "-窗口号码:" + param.getWindownum());
//                                SoundManager.getInstance().loadSoundWithCache(Tools.getApplicationByReflect(),param.getTakenum(),param.getWindownum());

                                SoundPlayUtils.newInstance().playJhSound(param.getTakenum(), param.getWindownum().substring(0, param.getWindownum().length() - 1), new ISoundCallback() {
                                    @Override
                                    public void startSoundPrint(String msg) {
                                        Tools.printLog("startSoundPrint", msg);
                                    }

                                    @Override
                                    public void failSoundPrint(String msg) {
                                        Tools.printLog("failSoundPrint", msg);

                                    }

                                    @Override
                                    public void finishSoundPrint(String msg) {
                                        Tools.printLog("finishSoundPrint", msg);

                                    }
                                });
                            }
                        } else {
                            LogUtils.d(jhAudioBean.getMessage());
                        }

                    }
                });
    }
}

//
//    @Override
//    public void onInit(int status) {
//        //设定语速 ，默认1.0正常语速
//        tts.setSpeechRate(0.78f);
//        tts.setPitch(1.6f);
//
//        // 判断是否转化成功
//        if (status == TextToSpeech.SUCCESS) {
//            //默认设定语言为中文，原生的android貌似不支持中文。
//            int result = tts.setLanguage(Locale.CHINESE);
//            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//                ToastUtils.showShort("不支持中文");
//            } else {
//                //不支持中文就将语言设置为英文
//                tts.setLanguage(Locale.US);
//            }
//        }
//    }

/**
 * stop不管是不是正在播放语音都会被打断
 */
//    public void release() {
//        if (tts != null) {
//            tts.stop();
//            tts.shutdown();
//        }
//    }
//}
